package org.foj.presenter {
import mx.controls.Alert;
import mx.managers.CursorManager;
import mx.messaging.ChannelSet;
import mx.rpc.AsyncResponder;
import mx.rpc.AsyncToken;
import mx.rpc.IResponder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

import org.foj.dto.User;
import org.foj.event.EventDispatcherFactory;
import org.foj.event.UserEvent;
import org.foj.model.ChannelSetFactory;
import org.foj.model.LoginModel;
import org.foj.view.LoginPanel;

public class LoginPresenter {

  private var _view:LoginPanel;
  private var _model:LoginModel;

  public function LoginPresenter(view:LoginPanel) {
    this._view = view;
    this._model = new LoginModel();

    EventDispatcherFactory.getEventDispatcher()
        .addEventListener(UserEvent.LOGIN_BUTTON_PRESSED,
        login);
    EventDispatcherFactory.getEventDispatcher()
        .addEventListener(UserEvent.LOGOUT_BUTTON_PRESSED,
        logout);
    EventDispatcherFactory.getEventDispatcher()
        .addEventListener(UserEvent.USER_LOGGED_IN,
        getUserDetails);

  }

  private function login(event:UserEvent = null):void {
    var defaultChannelSet:ChannelSet =
        ChannelSetFactory.getDefaultChannel();
    var responder:IResponder = new AsyncResponder(
        loginResult, handleError);
    _model.login(_view.usernameInput.text,
        _view.passwordInput.text, responder);
  }

  private function logout(event:UserEvent = null):void {
    var responder:IResponder = new AsyncResponder(
        logoutResult, handleError);

    _model.logout(responder);
  }

  private function getUserDetails(event:UserEvent = null):void {
    var responder:IResponder = new AsyncResponder(
        getUserDetailsResult, handleError);

    _model.getUserDetails(event.data, responder);
  }

  private function loginResult(event:ResultEvent,
                               token:Object = null):void {
    _view.loginStack.selectedChild = _view.loggedIn;
    _view.passwordInput.text = "";

    var userLoggedInEvent:UserEvent =
        new UserEvent(UserEvent.USER_LOGGED_IN);
    userLoggedInEvent.data = event.result.name;
    EventDispatcherFactory.getEventDispatcher()
        .dispatchEvent(userLoggedInEvent);
  }

  private function logoutResult(event:ResultEvent,
                                token:Object = null):void {
    _view.userName.text = "";
    _view.loginStack.selectedChild = _view.loggedOut;
    Alert.show("You have successfully logged out");

    var userChangedEvent:UserEvent =
        new UserEvent(UserEvent.CURRENT_USER_UPDATED);
    userChangedEvent.data = null;
    EventDispatcherFactory.getEventDispatcher()
        .dispatchEvent(userChangedEvent);
  }

  private function getUserDetailsResult(event:ResultEvent,
                                        token:Object = null):void {
    var user:User = new User(event.result);
    _view.userName.text = user.fullName;
    Alert.show("Welcome Back " + user.fullName);

    var userChangedEvent:UserEvent =
        new UserEvent(UserEvent.CURRENT_USER_UPDATED);
    userChangedEvent.data = user;
    EventDispatcherFactory.getEventDispatcher()
        .dispatchEvent(userChangedEvent);
  }

  private function handleError(event:FaultEvent,
                               token:AsyncToken = null):void {
    CursorManager.removeBusyCursor();
    Alert.show(event.fault.faultString);
  }

}
}